

package com.bei_en.teacher.admin.service.users;

import java.util.ArrayList;
import java.util.List;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.bei_en.teacher.admin.service.BaseService;
import com.bei_en.teacher.admin.model.users.bean.UsersDetails;
import com.bei_en.teacher.admin.model.users.vo.UsersDetailsVo;
import com.bei_en.teacher.admin.model.users.condition.UsersDetailsCondition;
import com.bei_en.teacher.admin.persistence.dao.users.UsersDetailsDao;

/**
 * （个人资料）
 * @author Guocg
 * @date 2016-7-27 10:03:24
 */
@Transactional(rollbackFor = Exception.class)
@Service
public class UsersDetailsService extends BaseService{

    @Autowired
    private UsersDetailsDao usersDetailsDao;

    public void insert(UsersDetailsVo model) {

    	 usersDetailsDao.insert(model.getUsersDetails());
    }

    /**
     * 插入并返回生成的id
     * @param model
     * @return
     */
    public Integer merge(UsersDetailsVo model) {

	usersDetailsDao.insert(model.getUsersDetails());
	return usersDetailsDao.findLastInsertedId();

    }

    public void delete(Integer modelInteger) {

    	usersDetailsDao.delete(modelInteger);
    }

    public UsersDetailsVo findById(Integer modelInteger) {

    	UsersDetailsVo model = new UsersDetailsVo(usersDetailsDao.findById(modelInteger));
    	return model;
    }

    public void update(UsersDetailsVo model) {

    	usersDetailsDao.update(model.getUsersDetails());
    }

    /**
     * 动态更新,只更新非null的值
     * @param model
     */
    public void updateSelective(UsersDetailsVo model) {

    	usersDetailsDao.updateSelective(model.getUsersDetails());
    }

    public int countAll() {

    	return usersDetailsDao.countAll();
    }

    public List<UsersDetailsVo> findAll() {

		List<UsersDetailsVo> list = new ArrayList<UsersDetailsVo>();
		List<UsersDetails> list1 = usersDetailsDao.findAll();
		for (UsersDetails model : list1) {
		    list.add(new UsersDetailsVo(model));
		}
		return list;
    }

    public List<Integer> findAllIds() {

    	return usersDetailsDao.findAllIds();
    }

    /**
     * 分页查询，包括总数和内容
     * @param Vo  返回总数
     * @return  返回内容
     */
    public List<UsersDetailsVo> findUsersDetailsPage(UsersDetailsCondition vo) {

		Integer totalCount = searchUsersDetailsNum(vo);
		List<UsersDetailsVo> list = new ArrayList<UsersDetailsVo>();
		vo.setTotalCount(totalCount);
	
		if (totalCount>0) {
		    vo.setOrderField(vo.getOrderField()!=null ? vo.getOrderField() : "id");
		    vo.setOrderDirection(vo.getOrderDirection()!=null ? vo.getOrderDirection() : "desc");
	
		    RowBounds rb = new RowBounds(vo.getStartIndex(), vo.getPageSize());
		    List<UsersDetails> list1 = usersDetailsDao.findPageBreakByCondition(vo, rb);
		    for (UsersDetails model : list1) {
		    	list.add(new UsersDetailsVo(model));
		    }
	
		}

		return list;
    }

    private Integer searchUsersDetailsNum(UsersDetailsCondition Vo) {

    	return usersDetailsDao.findNumberByCondition(Vo);
    }

}
